package com.ruoyi.qywx.service;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.constant.ErrorConstants;
import com.ruoyi.common.core.cache.Meta;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.CollectionUtils;
import com.ruoyi.common.utils.StrUtils;
import com.ruoyi.qywx.entity.ExtContact;
import com.ruoyi.qywx.entity.ExtFollowContact;
import com.ruoyi.qywx.getway.api.ExternalcontactApi;
import com.ruoyi.qywx.getway.entity.QyExtContactRemark;
import com.ruoyi.qywx.mapper.CustomerMapper;
import com.ruoyi.qywx.model.vo.BindTagVo;
import com.ruoyi.qywx.model.vo.CustomerDetailVo;
import com.ruoyi.qywx.model.vo.CustomerFollowerVo;
import com.ruoyi.qywx.model.vo.CustomerQueryVo;
import com.ruoyi.qywx.service.inters.CustomerService;
import com.ruoyi.qywx.service.inters.ExtContactService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author : hcl
 * @Date : 2023/8/30 14:29
 */
@Service
public class CustomerServiceImpl implements CustomerService {
    @Resource
    private CustomerMapper customerMapper;
    @Autowired
    private ExtContactService extContactService;

    @Override
    public PageInfo list(CustomerQueryVo vo, Long teamId) {
        List<Long> ids;
        // 简单的根据昵称搜索，直接走plus
        if (Objects.isNull(vo.getStatus())
                && StrUtils.isEmpty(vo.getStartAddTime())
                && StrUtils.isEmpty(vo.getEndAddTime())
                && StrUtils.isEmpty(vo.getDelStartTime())
                && StrUtils.isEmpty(vo.getDelEndTime())) {
            ids = extContactService.simpleTmAndNameQueryExtConcatIds(vo.getKeyword(), teamId);
        } else {
            // 复杂的搜索走原生sql
            ids = customerMapper.findCustomerIdsByQuery(vo, teamId);
        }
        PageInfo page = new PageInfo(ids);
        if (CollectionUtils.isNotEmpty(ids)) {
            page.setList(customerMapper.findCustomerList(ids));
        }
        return page;
    }

    @Override
    public AjaxResult findCustomerById(Long id, Long teamId) {
        ExtContact extContact = extContactService.selectContactById(id);
        if (extContact == null || !extContact.getTeamId().equals(teamId)) {
            return AjaxResult.error(ErrorConstants.NO_DATA);
        }
        List<CustomerFollowerVo> followers = extContactService.selectFollowersByContactId(extContact.getExtUserId());
        CustomerDetailVo vo = new CustomerDetailVo();
        vo.setCustomer(extContact);
        vo.setFollowers(followers);
        return AjaxResult.success(vo);
    }

    @Override
    public AjaxResult customerBindTags(BindTagVo bind, Meta meta) {
        boolean success = extContactService.bindFollowerTags(bind, meta);
        return success ? AjaxResult.success() : AjaxResult.error("绑定失败");
    }

    @Override
    public void editRemark(CustomerFollowerVo vo, Meta teamMeta) {
        ExtFollowContact follower = extContactService.selectFollower(vo.getExtUserId(), vo.getUserId());
        if (Objects.isNull(follower)) return;
        boolean success = ExternalcontactApi.editExternalContactRemark(teamMeta, vo);
        if (success) {
            extContactService.updateFollowerById(QyExtContactRemark
                    .builder()
                    .userid(vo.getUserId())
                    .external_userid(vo.getExtUserId())
                    .remark(vo.getRemark())
                    .remark_mobiles(CollectionUtils.toStrList(vo.getRemarkMobiles()))
                    .remark_company(vo.getRemarkCorpName())
                    .description(vo.getDescription())
                    .build());
        }
    }
}
